<?php

namespace app\controller\uc;

use core\controller\UcBase;
use app\bll\UserMessage as BllUserMessage;
use Exception;
use support\View;

class UserMessage extends UcBase
{
    /**
     * 系统消息
     *
     * @return mixed
     */
    public function system()
    {

        $bllUserMessage = new BllUserMessage();
        $messages       = $bllUserMessage->paginator(request()->userinfo['id']);

        //先直接设置成已读
        $ids = [];
        foreach ($messages as $key => $message) {
            $ids[] = $message->id;
        }

        if ($ids) {
            $bllUserMessage->setRead($ids, request()->userinfo['id']);
        }

        View::assign('messages', $messages);

        return $this->view();
    }


    /**
     * 删除消息
     *
     * @return mixed
     */
    public function delete()
    {
        try {
            $bllUserMessage = new BllUserMessage();
            $bllUserMessage->delete(request()->post('ids'), request()->userinfo['id']);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 全部标记已读
     *
     * @return mixed
     */
    public function setAllRead()
    {
        try {
            $bllUserMessage = new BllUserMessage();
            $bllUserMessage->setAllRead(request()->userinfo['id']);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
